<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateQuestionnaireFieldsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('questionnaire_fields', function (Blueprint $table) {
            $table->id()->from(config('migration.from'));

            $table->string('key')->comment('Ключ поля');

            $table->string('type_key')->unsigned()->comment('Тип поля');
            $table->foreign('type_key')->references('key')->on('types')
                ->onDelete('restrict')->onUpdate('restrict');

            $table->text('label')->nullable()->comment('Описание');
            $table->string('placeholder')->nullable()->comment('Подсказка');
            $table->jsonb('options')->nullable()->comment('select');
            $table->jsonb('default')->default(json_encode(['']));

            $table->integer('order')->default(0)->comment('Сортировка');

            $table->jsonb('depends_key')->default(json_encode([]))->comment('Зависит от ключей');
            $table->jsonb('depends_value')->default(json_encode([]))->comment('Зависит от значений');

            $table->bigInteger('form_id')->unsigned();
            $table->foreign('form_id')->references('id')->on('questionnaire_forms')->onDelete('restrict');

            $table->boolean('required')->default(false)->comment('Обязательный');

            $table->unique(['key', 'form_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('questionnaire_fields');
    }
}
